package com.jiyinit.web.core.business.controller;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.web.core.business.dto.ResourceRequest;
import com.jiyinit.web.core.business.dto.TreePageRequest;
import com.jiyinit.web.core.business.entity.TabCoreResourceEntity;
import com.jiyinit.web.core.business.enums.ResourceTypeEnum;
import com.jiyinit.web.core.business.service.ResourceServiceImpl;
import com.jiyinit.web.core.common.logic.ParamCheckLogic;
import com.jiyinit.web.core.common.logic.ValidateResult;
import com.jiyinit.web.core.common.util.UserUtil;
import com.jiyinit.web.core.common.vo.CurrentUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author lishaochuan
 * @version 1.0
 * @date 2017/4/17 9:01
 * @since 1.0
 */
@Controller
@RequestMapping("/resource")
public class ResourceController {


    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ResourceController.class);

    @Resource(name = "core.paramCheckLogic")
    private ParamCheckLogic paramCheckLogic;

    @Resource(name = "core.resourceServiceImpl")
    ResourceServiceImpl resourceService;

    /**
     * 菜单管理页面
     * @author lishaochuan
     * @create 2017/4/19 16:54
     * @param
     * @return
     */
    @RequestMapping("/toTreeMenu")
    public String toTreeMenu() {
        return "/resource/treeMenu";
    }


    /**
     * 查询菜单树
     * @author lishaochuan
     * @create 2017/4/17 15:16
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/getTreeByParent")
    public ResponseEntity<ResponseDto> getTreeByParent(Integer parentId){
        if(Check.isNullObj(parentId)){
            parentId = 0;
        }
        List<TabCoreResourceEntity> tabCoreResources = resourceService.selectByParentId(parentId);
        List items = new ArrayList<>();
        for (TabCoreResourceEntity tabCoreResource : tabCoreResources) {
            Map map = new HashMap();
            map.put("id", tabCoreResource.getResourceId());
            map.put("pId", tabCoreResource.getParentId());
            map.put("name", tabCoreResource.getResourceName());
            if(ResourceTypeEnum.FUNCTION.getCode().equals(tabCoreResource.getResourceType())){
                map.put("isParent", false);
            } else {
                map.put("isParent", true);
            }
            items.add(map);
        }
        return new ResponseEntity<>(ResponseDto.responseOK(items), HttpStatus.OK);
    }


    /**
     * 分页根据parentId查询子节点树
     * @author lishaochuan
     * @create 2017/4/17 17:46
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/getTreeByParentForPage")
    @ResponseBody
    public PagingResult getTreeByParentForPage(TreePageRequest treePageRequest){
        PagingResult<TabCoreResourceEntity> pagingResult = resourceService.selectPageByParentId(treePageRequest);
        return pagingResult;
    }


    /**
     * 跳转到添加菜单页面
     * @author lishaochuan
     * @create 2017/4/17 17:47
     * @param
     * @return
     */
    @RequestMapping(value = "/toTreeAdd")
    public String toTreeAdd(HttpServletRequest request, Integer parentId) {
        request.setAttribute("parentId", parentId);
        return "/resource/treeAdd";
    }


    /**
     * 添加菜单
     * @author lishaochuan
     * @create 2017/4/18 10:01
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/treeAdd")
    @ResponseBody
    public ResponseEntity<ResponseDto> treeAdd(ResourceRequest resourceRequest){
        // 校验参数
        ValidateResult<ResourceRequest> validateResult = paramCheckLogic.checkParamValidate(resourceRequest);
        if (!validateResult.isSuccess()) {
            return new ResponseEntity<>(validateResult.getDto(), HttpStatus.OK);
        }
        TabCoreResourceEntity tabCoreResourceEntity = new TabCoreResourceEntity();
        BeanUtils.copyProperties(resourceRequest, tabCoreResourceEntity);

        TabCoreResourceEntity parentResource = resourceService.selectById(tabCoreResourceEntity.getParentId());
        tabCoreResourceEntity.setResourceLevel(parentResource.getResourceLevel() + 1);

        CurrentUserVo currentUser = UserUtil.getCurrentUser();
        Integer userId = currentUser.getUserId();
        tabCoreResourceEntity.setCreateId(userId);
        tabCoreResourceEntity.setCreateTime(new Date());
        int num = resourceService.save(tabCoreResourceEntity);
        if(num == 1){
            return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
        }
        return new ResponseEntity<>(ResponseDto.responseFail("系统错误"), HttpStatus.OK);
    }


    /**
     * 跳转修改菜单页面
     * @author lishaochuan
     * @create 2017/4/19 15:35
     * @param
     * @return
     */
    @RequestMapping(value = "/toTreeUpdate")
    public String toTreeUpdate(HttpServletRequest request, Integer resourceId) {
        TabCoreResourceEntity coreResource = resourceService.selectById(resourceId);
        request.setAttribute("resource", coreResource);
        return "/resource/treeUpdate";
    }


    /**
     * 修改菜单
     * @author lishaochuan
     * @create 2017/4/19 15:37
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/treeUpdate")
    @ResponseBody
    public ResponseEntity<ResponseDto> treeUpdate(ResourceRequest resourceRequest){
        // 校验参数
        ValidateResult<ResourceRequest> validateResult = paramCheckLogic.checkParamValidate(resourceRequest);
        if (!validateResult.isSuccess()) {
            return new ResponseEntity<>(validateResult.getDto(), HttpStatus.OK);
        }
        TabCoreResourceEntity tabCoreResourceEntity = new TabCoreResourceEntity();
        BeanUtils.copyProperties(resourceRequest, tabCoreResourceEntity);

        CurrentUserVo currentUser = UserUtil.getCurrentUser();
        Integer userId = currentUser.getUserId();
        tabCoreResourceEntity.setLastModifyId(userId);
        tabCoreResourceEntity.setLastModifyTime(new Date());
        int num = resourceService.update(tabCoreResourceEntity);
        if(num == 1){
            return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
        }
        return new ResponseEntity<>(ResponseDto.responseFail("系统错误"), HttpStatus.OK);
    }


}
